package cn.edu.ctbu.firstdata.controller;

import cn.edu.ctbu.firstdata.domain.Student;
import cn.edu.ctbu.firstdata.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;
    /**
     * 渲染ajaxlist.html
     * @return
     */
    @GetMapping("ajaxlistold")
    public String ajaxlist(){
        return "student/ajaxlist";
    }

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("ajaxlist")
    public String list(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        if(session.getAttribute("userid") == null){
            return "redirect:/login/user";
        }
        List<Student> students = studentService.getAll();
        model.addAttribute("data", students);
        return "/student/ajaxlist";
    }
}
